﻿using CloudFranceTriple.Models;
using System.Data;
using System.Linq;
using System.Web.Mvc;

namespace CloudFranceTriple.WebRole.Areas.Forum.Controllers
{
    [CloudFranceTriple.WebRole.Filters.InitializeSimpleMembership]
    public class CategoryController : Controller
    {
        private cloudfrAboq2RsDMEntities db = new cloudfrAboq2RsDMEntities();

        //
        // GET: /Forum/Category/MoveUp/5

        [Authorize(Roles = "RoleAdminForum")]
        public ActionResult MoveUp(int id = 0)
        {
            ForumCategorySet forumcategoryset = db.ForumCategorySet.Find(id);
            if (forumcategoryset == null)
            {
                return HttpNotFound();
            }

            if (forumcategoryset.OrderForumCategory == 10)
                return RedirectToAction("Index", "Forum");

            var forumCategoryAbove = db.ForumCategorySet.Where(c1 => c1.Forum_Id == forumcategoryset.Forum_Id).Where(c2 => c2.OrderForumCategory == (forumcategoryset.OrderForumCategory - 10)).First();
            forumCategoryAbove.OrderForumCategory = forumcategoryset.OrderForumCategory;
            forumcategoryset.OrderForumCategory -= 10;

            if (ModelState.IsValid)
            {
                db.Entry(forumcategoryset).State = EntityState.Modified;
                db.Entry(forumCategoryAbove).State = EntityState.Modified;
                db.SaveChanges();
            }
            return RedirectToAction("Index", "Forum");
        }

        //
        // GET: /Forum/Category/MoveDown/5

        [Authorize(Roles = "RoleAdminForum")]
        public ActionResult MoveDown(int id = 0)
        {
            ForumCategorySet forumcategoryset = db.ForumCategorySet.Find(id);
            if (forumcategoryset == null)
            {
                return HttpNotFound();
            }

            if (forumcategoryset.OrderForumCategory == db.ForumCategorySet.Where(c1 => c1.Forum_Id == forumcategoryset.Forum_Id).Max(f1 => f1.OrderForumCategory))
                return RedirectToAction("Index", "Forum");

            var forumBelow = db.ForumCategorySet.Where(c1 => c1.Forum_Id == forumcategoryset.Forum_Id).Where(c2 => c2.OrderForumCategory == (forumcategoryset.OrderForumCategory + 10)).First();
            forumBelow.OrderForumCategory = forumcategoryset.OrderForumCategory;
            forumcategoryset.OrderForumCategory += 10;

            if (ModelState.IsValid)
            {
                db.Entry(forumcategoryset).State = EntityState.Modified;
                db.Entry(forumBelow).State = EntityState.Modified;
                db.SaveChanges();
            }
            return RedirectToAction("Index", "Forum");
        }
        //
        // GET: /Forum/Category/Create

        [Authorize(Roles = "RoleAdminForum")]
        public ActionResult Create(int id = 0)
        {
            ViewBag.Forum_Id = new SelectList(db.ForumSet, "Id", "LabelForum", id);
            return View();
        }

        //
        // POST: /Forum/Category/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminForum")]
        [ValidateInput(false)]
        public ActionResult Create(ForumCategorySet forumcategoryset)
        {
            if (ModelState.IsValid)
            {
                if (db.ForumCategorySet.Where(c1 => c1.Forum_Id == forumcategoryset.Forum_Id).Count() == 0)
                    forumcategoryset.OrderForumCategory = 10;
                else
                    forumcategoryset.OrderForumCategory = db.ForumCategorySet.Where(c1 => c1.Forum_Id == forumcategoryset.Forum_Id).Max(c1 => c1.OrderForumCategory) + 10;

                db.ForumCategorySet.Add(forumcategoryset);
                db.SaveChanges();
                return RedirectToAction("Index", "Topic", new { id = forumcategoryset.Id });
            }

            ViewBag.Forum_Id = new SelectList(db.ForumSet, "Id", "LabelForum", forumcategoryset.Forum_Id);
            return View(forumcategoryset);
        }

        //
        // GET: /Forum/Category/Edit/5

        [Authorize(Roles = "RoleAdminForum")]
        public ActionResult Edit(int id = 0)
        {
            ForumCategorySet forumcategoryset = db.ForumCategorySet.Find(id);
            if (forumcategoryset == null)
            {
                return HttpNotFound();
            }
            ViewBag.Forum_Id = new SelectList(db.ForumSet, "Id", "LabelForum", forumcategoryset.Forum_Id);
            return View(forumcategoryset);
        }

        //
        // POST: /Forum/Category/Edit/5

        //Todo : recalculer OrderCategory si la catégorie change de forum.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminForum")]
        [ValidateInput(false)]
        public ActionResult Edit(ForumCategorySet forumcategoryset)
        {
            if (ModelState.IsValid)
            {
                db.Entry(forumcategoryset).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("", "Forum", null);
            }
            ViewBag.Forum_Id = new SelectList(db.ForumSet, "Id", "LabelForum", forumcategoryset.Forum_Id);
            return View(forumcategoryset);
        }

        //
        // GET: /Forum/Category/Delete/5

        [Authorize(Roles = "RoleAdminForum")]
        public ActionResult Delete(int id = 0)
        {
            ForumCategorySet forumcategoryset = db.ForumCategorySet.Find(id);
            if (forumcategoryset == null)
            {
                return HttpNotFound();
            }
            return View(forumcategoryset);
        }

        //
        // POST: /Forum/Category/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminForum")]
        public ActionResult DeleteConfirmed(int id)
        {
            ForumCategorySet forumcategoryset = db.ForumCategorySet.Find(id);

            if (forumcategoryset.OrderForumCategory < db.ForumCategorySet.Where(c1 => c1.Forum_Id == forumcategoryset.Forum_Id).Max(c2 => c2.OrderForumCategory))
            {
                var forumCategoryToMoveDown = db.ForumCategorySet.Where(c1 => c1.Forum_Id == forumcategoryset.Forum_Id).Where(C2 => C2.OrderForumCategory > forumcategoryset.OrderForumCategory).ToList();
                foreach (ForumCategorySet forumCat in forumCategoryToMoveDown)
                    forumCat.OrderForumCategory -= 10;
            }

            db.ForumCategorySet.Remove(forumcategoryset);
            db.SaveChanges();
            return RedirectToAction("Index", "Forum");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}